package com.lizhi.parse;

import java.util.Calendar;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.lizhi.model.BookModel;
import com.lizhi.util.Util;

public class ParseBook {
	private String host = "http://www.sbkk8.cn";
	private String url = "http://www.sbkk8.cn/lizhishu/";

	public ParseBook() {
//		 Util.clearData(Util.getBookURL()+"books.json");
		try {
			Document document = Util.getDocument(url);
			Elements elements = document.getElementsByClass("ablum");
			int seed = 126060;
			Calendar calendar = Calendar.getInstance();
			calendar.set(2015, 4, 23);

			for (int i = (elements.size()-1); i >0; i--) {
				Element element = elements.get(i);
				String href = host + element.attr("href");
				String img = element.getElementsByTag("img").attr("src");
				String name = element.getElementsByTag("img").attr("alt");

				BookModel bookModel = new BookModel();
				bookModel.name = name;
				if (!img.contains(host)) {
					bookModel.img = host + img;
				} else {
					bookModel.img = img;
				}

				bookModel.detail = href;
				bookModel.type = "lizhi";
				bookModel.id = String.valueOf(seed);
				bookModel.time = String.valueOf(calendar.getTimeInMillis());
				calendar.add(Calendar.MILLISECOND, 1);
				if (i % 4 == 0) {
					calendar.add(Calendar.WEEK_OF_MONTH, 1);
				}
				System.out.println("parse book " + bookModel.name + " finish!");
				Thread.sleep(2000);
				Util.execute(new ParseBookDetail(bookModel, href));
				seed++;
			}

		} catch (Exception e) {
			e.printStackTrace();
		} 
	}

	public static void main(String[] args) {
		new ParseBook();
	}

}
